<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const password = ref<string>('')
const maxLen = 6
const showSuccess = ref<boolean>(false)

function inputNum(num: number): void {
    if (password.value.length < maxLen) {
        password.value += num.toString()
    }
}
function delNum(): void {
    password.value = password.value.slice(0, -1)
}
function goBack(): void {
    router.back()
}
function submit(): void {
    if (password.value.length === maxLen) {
        showSuccess.value = true
    }
}
function goOrder(): void {
    // 跳转到订单页
    router.push('/OrderConfirmed1')
}
function goHome(): void {
    router.push('/')
}
</script>

<template>
    <div class="pay-tj">
        <div class="header">
            <span class="back" @click="goBack">←</span>
            <span class="title">输入支付密码</span>
        </div>
        <div class="tip">请输入支付密码</div>
        <div class="pwd-box">
            <div v-for="i in maxLen" :key="i" class="pwd-cell" :class="{ active: password.length + 1 === i }">
                <span v-if="i <= password.length" class="dot"></span>
            </div>
        </div>
        <button class="submit-btn" :disabled="password.length !== maxLen" @click="submit">继续</button>
        <div class="keyboard">
            <div class="key-row" v-for="row in [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 'del']]" :key="row.join()">
                <button v-for="key in row" :key="key" class="key" @click="key === 'del' ? delNum() : inputNum(key)"
                    :class="{ del: key === 'del' }">
                    <template v-if="key === 'del'">
                        <svg width="24" height="24" viewBox="0 0 24 24">
                            <rect x="5" y="7" width="10" height="10" rx="2" fill="#bbb" />
                            <path d="M9 10l2 2m0 0l2 2m-2-2l-2 2m2-2l2-2" stroke="#fff" stroke-width="1.5"
                                stroke-linecap="round" />
                        </svg>
                    </template>
                    <template v-else>{{ key }}</template>
                </button>
            </div>
        </div>
        <!-- 支付成功弹窗 -->
        <div v-if="showSuccess" class="pay-success-mask">
            <div class="pay-success-dialog">
                <div class="pay-success-icon">
                    <svg width="64" height="64" viewBox="0 0 64 64" fill="none">
                        <circle cx="32" cy="32" r="32" fill="#111" />
                        <path d="M20 34l9 9 15-17" stroke="#fff" stroke-width="4" stroke-linecap="round"
                            stroke-linejoin="round" />
                    </svg>
                </div>
                <div class="pay-success-title">支付成功</div>
                <button class="pay-success-btn main" @click="goOrder">查看订单</button>
                <button class="pay-success-btn" @click="goHome">返回主页</button>
            </div>
        </div>
    </div>
</template>

<style scoped>
.pay-tj {
    background: #fff;
    min-height: 100vh;
    font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
    padding-bottom: 32px;
}

.header {
    display: flex;
    align-items: center;
    height: 56px;
    padding: 0 0 0 12px;
    font-size: 20px;
    font-weight: bold;
    position: relative;
    background: #fff;
    justify-content: center;
}

.back {
    font-size: 26px;
    margin-right: 8px;
    cursor: pointer;
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
}

.title {
    flex: 1;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
}

.tip {
    margin: 48px 0 32px 0;
    text-align: center;
    color: #222;
    font-size: 18px;
    font-weight: 500;
}

.pwd-box {
    display: flex;
    justify-content: center;
    gap: 18px;
    margin-bottom: 48px;
}

.pwd-cell {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: #fafbfc;
    border: 2px solid #eee;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    font-weight: bold;
    box-sizing: border-box;
    transition: border 0.2s;
}

.pwd-cell.active {
    border: 2px solid #111;
    background: #fff;
}

.dot {
    width: 16px;
    height: 16px;
    background: #111;
    border-radius: 50%;
    display: inline-block;
}

.submit-btn {
    width: 92vw;
    max-width: 600px;
    height: 56px;
    background: #111;
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    border: none;
    border-radius: 28px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
    cursor: pointer;
    letter-spacing: 2px;
    margin: 32px auto 0 auto;
    display: block;
    transition: background 0.2s;
}

.submit-btn:disabled {
    background: #bbb;
    color: #fff;
    cursor: not-allowed;
}

.keyboard {
    margin: 32px auto 0 auto;
    max-width: 400px;
    user-select: none;
}

.key-row {
    display: flex;
    justify-content: center;
    gap: 18px;
    margin-bottom: 18px;
}

.key {
    width: 80px;
    height: 56px;
    background: #f7f7f7;
    border: none;
    border-radius: 16px;
    font-size: 22px;
    font-weight: 600;
    color: #222;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.key:active {
    background: #eee;
}

.key.del {
    background: #fbeaea;
}

.key.del:active {
    background: #f8d7da;
}

@media (max-width: 500px) {
    .key {
        width: 60px;
        height: 48px;
        font-size: 20px;
    }

    .pwd-cell {
        width: 38px;
        height: 38px;
    }
}
.pay-success-mask {
    position: fixed;
    left: 0; right: 0; top: 0; bottom: 0;
    background: rgba(0,0,0,0.38);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}
.pay-success-dialog {
    background: #fff;
    border-radius: 32px;
    padding: 48px 24px 32px 24px;
    min-width: 300px;
    max-width: 90vw;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 8px 32px 0 rgba(0,0,0,0.18), 0 1.5px 6px 0 rgba(0,0,0,0.08);
}
.pay-success-icon {
    margin-bottom: 24px;
}
.pay-success-title {
    font-size: 22px;
    font-weight: bold;
    color: #111;
    margin-bottom: 36px;
    text-align: center;
}
.pay-success-btn {
    width: 240px;
    max-width: 80vw;
    height: 48px;
    border: none;
    border-radius: 24px;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 18px;
    background: #f2f2f2;
    color: #222;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}
.pay-success-btn.main {
    background: #111;
    color: #fff;
    margin-bottom: 16px;
}
.pay-success-btn:last-child {
    margin-bottom: 0;
}
</style>